package com.phuc.game.andPuzzle.moreGame;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.phuc.game.andPuzzle.R;

public class LazyAdapter extends BaseAdapter {
    
	ImageLoader imageLoader = ImageLoader.getInstance();
    private Activity activity;
    private List<Item> data;
    private static LayoutInflater inflater=null;
    DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showStubImage(R.drawable.stub)
			.showImageForEmptyUri(R.drawable.stub)
			.showImageOnFail(R.drawable.stub)
			.cacheInMemory(true)
			.cacheOnDisc(true)
			.bitmapConfig(Config.RGB_565)
			.build();
    
    public LazyAdapter(Activity a, List<Item> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public static class ViewHolder{
        public TextView text;
        public ImageView image;
        public ImageView isHotImg;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.item_more_game, null);
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.text);;
            holder.image=(ImageView)vi.findViewById(R.id.image);
            holder.isHotImg=(ImageView)vi.findViewById(R.id.isHot);
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();
        Log.d("Chung",""+data.get(position).getLinkImage());
        Log.d("Chung",""+!TextUtils.isEmpty(data.get(position).getLinkImage()));
        holder.text.setText(data.get(position).getNameApp());
        
        if(!TextUtils.isEmpty(data.get(position).getLinkImage())){
        	Log.d("Chung","!null:"+data.get(position).getLinkImage());
        	holder.image.setTag(data.get(position).getLinkImage());
        	imageLoader.displayImage(data.get(position).getLinkImage(), holder.image, options);
        }
        if(!TextUtils.isEmpty(data.get(position).getIsHot())){
        	if(data.get(position).getIsHot().equals("true")){
        		holder.isHotImg.setVisibility(View.VISIBLE);
        		final AnimationDrawable frameAnimation = (AnimationDrawable)holder.isHotImg.getDrawable();
        		holder.isHotImg.post(new Runnable() {
        		    public void run() {
        		        if ( frameAnimation != null ) frameAnimation.start();
        		      }
        		});
        	}
        }
        return vi;
    }
}